package hDOJ;


/*
1 3 0 3 2 5  6  4  10 8  15 15
3 4 7 8 9 10 12 14 15 18 19 20

12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0





 */


import java.util.Scanner;

public class Main2037 {
	
    public static void main(String[] args) {
    	
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        int[] begin = new int[n];
        int[] end = new int[n];
        int[] min = new int[n];
        int count = 0;
        for(int i = 0; i < n; i++) {
        	begin[i] = sc.nextInt();
        	end[i] = sc.nextInt();
        }//选出第一个时间段，要求起始时间大于等于0，终止时间最小
        min[count] = 23;//初始终止时间最小值
        for(int i = 0; i < n; i++) {
            if(begin[i] > 0) {
                if(end[i] < min[count]) {
                    min[count] = end[i];
                }
            }
        }
        System.out.println(min[count] + "," + count);
        count++;
        find(n, begin, end, min, count);
        
    }

    private static void find(int n, int[] begin, int[] end, int[] min, int count) {
        // TODO Auto-generated method stub
        min[count] = 23;
        for(int i = 0; i < n; i++) {
            if(begin[i] >= min[count-1]) {
                if(end[i] < min[count]) {
                    min[count] = end[i];
                }
            }
        }
        System.out.println(min[count] + "," + count);
        if(min[count-1] == min[count]) {
            System.out.println(count-1);
        }else if(min[count-1] != min[count]) {
            count++;
            find(n, begin, end, min, count);
        }
    }
}
